﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mates
{
    public static class RLE
    {
        ///<summary>
        ///Compresió RLE per a strings, format 1A2B3C4D...
        ///</summary>
        public static string Encode(int x, int y, int z, string input)
        {
            string strOut = null;
            for (int i = 0; i < input.Length; i++)
            {
                char symbol = input[i];
                int count = 1;
                for (int j = i; j < input.Length - 1; j++)
                {
                    if (input[j + 1] != symbol)
                        break;
                    count++;
                    i++;
                }
                strOut += count.ToString();
                strOut += symbol;
            }
            return strOut;

        }
        public static string Decode(int x, int y, int z,string input)
        {
            string strOut = null;
            int count = 0;
            string qt = null;

            for (int i = 0; i <input.Length; i++)
            {

                if (char.IsNumber(input[i]))
                {
                    qt += input[i];
                }
                else
                {
                    if (qt == null)
                        break;
                    count = Int32.Parse(qt);
                    if (count > 0)
                    {
                        for (int k = 0; k < count; k++)
                        {
                            strOut += input[i];
                        }
                    }
                    count = 0;
                    qt = null;
                }
            }

            return strOut;
        }
    }
}
